﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IQKS.AWP.EmailManager
{
    public  class EmailManagerDAL
    {
        public EmailServerBO GetEmailServer(params object[] param)
        {             
            EmailServerBO objEmailServerBO = new EmailServerBO();
            try
            {
                DataSet objDs = DataUtils.ExecuteDataset("Usp_GetEmailServer");
                if (objDs.Tables.Count > 0)
                {
                    objEmailServerBO.EmailServerID = Convert.ToInt32(objDs.Tables[0].Rows[0]["EmailServerID"]);
                    objEmailServerBO.SMTPServer = Convert.ToString(objDs.Tables[0].Rows[0]["SMTPServer"]);
                    objEmailServerBO.SMTPServerEmail = Convert.ToString(objDs.Tables[0].Rows[0]["SMTPServerEmail"]);
                    objEmailServerBO.SMTPServerPassword = Convert.ToString(objDs.Tables[0].Rows[0]["SMTPServerPassword"]);
                    objEmailServerBO.SiteURL = Convert.ToString(objDs.Tables[0].Rows[0]["SiteURL"]);
                    objEmailServerBO.IsActive = Convert.ToBoolean(objDs.Tables[0].Rows[0]["IsActive"]);
                }
            }
            catch (Exception exception)
            {
                throw new ApplicationException(exception.Message);
            }
            return objEmailServerBO;
        }

        public DataSet GetRecordsForSendingEmails(params object[] param)
        {
            DataSet objDs;

            try
            {
                objDs = DataUtils.ExecuteDataset("Usp_GETRecordsForSendingEmails");

            }
            catch (Exception exception)
            {
                throw new ApplicationException(exception.Message);
            }
            return objDs;
        }

        public object SetEmailSentStatus(EmailManagerBO param)
        {
            try
            {

                SqlParameter[] parameters = new SqlParameter[]
               {
                  new SqlParameter("@SetMethod","UPDATE"),
                  new SqlParameter("@EmailQueueID", param.EmailQueueID),
                  new SqlParameter("@EmailSentCode", param.EmailSentCode),
                  new SqlParameter("@EmailErrorMsg",param.EmailErrorMsg)            
               };
                return DataUtils.ExecuteScalar("Usp_SetEmailSentStatus", parameters);
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ex.Message);
            }
        }
    }
}
